﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace YF_Music.Core.Converters
{
  /// <summary>
  /// 将宽度和高度转换为EllipseGeometry以进行剪裁。
  /// Converts width and height to an EllipseGeometry for clipping purposes.
  /// </summary>
  public class EllipseClipConverter : IMultiValueConverter
  {
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
      if (values.Length < 4) return null;

      double width = GetValid(values[0], values[2]); // Width or ActualWidth
      double height = GetValid(values[1], values[3]); // Height or ActualHeight
      if (width <= 0 || height <= 0) return null;

      return new EllipseGeometry(new System.Windows.Point(width / 2, height / 2), width / 2 - 0.05, height / 2 - 0.05);
    }

    private double GetValid(object primary, object fallback)
    {
      double val = primary is double d && !double.IsNaN(d) ? d : -1;
      if (val > 0) return val;
      return fallback is double f && !double.IsNaN(f) ? f : 0;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        => throw new NotSupportedException();
  }
}
